<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Monitor CAN channel</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="kvaser.gif"/></td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('page_example_c_candump.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Monitor CAN channel </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>samples\example\c\candump.c</b> </p><div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">** This software is furnished as Redistributable under the Kvaser Software Licence</span></div><div class="line"><span class="comment">** https://www.kvaser.com/canlib-webhelp/page_license_and_copyright.html</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">** Description:</span></div><div class="line"><span class="comment">** This is a sample console program that dumps the traffic on the CAN bus on</span></div><div class="line"><span class="comment">** the screen, or to a file. The purpuse of this program is to demonstrate</span></div><div class="line"><span class="comment">** certain programming techniques. It may or may not be useful to you.</span></div><div class="line"><span class="comment">**</span></div><div class="line"><span class="comment">*/</span></div><div class="line"></div><div class="line"><span class="comment">// First, include the whole known universe.</span></div><div class="line"><span class="preprocessor">#include &lt;stdlib.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;memory.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;windows.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;conio.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;time.h&gt;</span></div><div class="line"></div><div class="line"><span class="comment">// CANLIB requires the following #include.</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="canlib_8h.html">canlib.h</a>&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> Usage(<span class="keywordtype">void</span>)</div><div class="line">{</div><div class="line">  printf(<span class="stringliteral">&quot;\nCANLIB Dump Program\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;(Part of the CANLIB SDK from KVASER AB - http://www.kvaser.se)\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;This is a sample program that just dumps all incoming messages on the screen,\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;or to a file.\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;\nUsage: candump [flags] [filename]\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;   -X          Listen to CAN channel number X.\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;   -B&lt;value&gt;   Set the bitrate. Value is any of 1000,500,250,125.\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;   -A&lt;value&gt;   Set the CAN FD Arbitration bitrate. Value is any of 500,1000.\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;   -D&lt;value&gt;   Set the CAN FD Data bitrate. Value is any of 1000,2000,4000,8000.\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;               Default bitrate is 125 kbit/s.\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;   -h          Print this help text.\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;   -q          Be more quiet than usual.\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;   -fd         Use CAN FD.\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;   -virtual    Accept virtual channels.\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;\nIf no filename is specified, standard output is used.\n\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;The following keys can be used during logging:\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;   ESC         Stop logging.\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;   Q           Be more quiet.\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;   q           Be less quiet.\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;\nExample:\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;candump -B250 -0 logfile.log\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;   would set CAN channel 0 to 250 kbit/s and log to logfile.log\n&quot;</span>);</div><div class="line"></div><div class="line">  exit(1);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// Global variables for the command-line options.</span></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="keywordtype">int</span>  Bitrate = 0;          <span class="comment">// Selected bit rate.</span></div><div class="line"><span class="keywordtype">int</span>  BitrateFdA = 0;       <span class="comment">// Selected CAN FD Arbritation bit rate.</span></div><div class="line"><span class="keywordtype">int</span>  BitrateFdD = 0;       <span class="comment">// Selected CAN FD Data bit rate.</span></div><div class="line"><span class="keywordtype">int</span>  Source = 0;           <span class="comment">// Channel #</span></div><div class="line"><span class="keywordtype">char</span> Filename[512];        <span class="comment">// Name of log file</span></div><div class="line"><span class="keywordtype">int</span>  Quiet = 0;            <span class="comment">// Defines the verbosity.</span></div><div class="line"><span class="keywordtype">int</span>  can_open_flags = 0;   <span class="comment">// Flags for canOpen, e.g. set CAN FD.</span></div><div class="line"></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// Check a status code and issue an error message if the code isn&#39;t canOK.</span></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="keywordtype">void</span> ErrorExit(<span class="keywordtype">char</span>* <span class="keywordtype">id</span>, <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat)</div><div class="line">{</div><div class="line">  <span class="keywordflow">if</span> (stat != <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">    <span class="keywordtype">char</span> buf[50];</div><div class="line">    buf[0] = <span class="charliteral">&#39;\0&#39;</span>;</div><div class="line">    <a class="code" href="group__can__general.html#ga01a7a415c95c579750bcdd95a1d245c4">canGetErrorText</a>(stat, buf, <span class="keyword">sizeof</span>(buf));</div><div class="line">    fprintf(stderr, <span class="stringliteral">&quot;%s: failed, stat=%d (%s)\n&quot;</span>, <span class="keywordtype">id</span>, (<span class="keywordtype">int</span>)stat, buf);</div><div class="line">    exit(1);</div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// Setup a CAN controller.</span></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="keywordtype">int</span> InitCtrl(<span class="keywordtype">int</span> ctrl)</div><div class="line">{</div><div class="line">  <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat;</div><div class="line">  <a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a> hnd;</div><div class="line"></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="comment">// First, open a handle to the CAN circuit. (We could specify</span></div><div class="line">  <span class="comment">// canOPEN_EXCLUSIVE to ensure we get a circuit that noone else</span></div><div class="line">  <span class="comment">// is using, if desired.)</span></div><div class="line">  <span class="comment">//</span></div><div class="line">  hnd = <a class="code" href="group___c_a_n.html#gac377d182232fb4ec2fed881c2b9ab300">canOpenChannel</a>(ctrl, can_open_flags);</div><div class="line">  <span class="keywordflow">if</span> (hnd &lt; 0) {</div><div class="line">    ErrorExit(<span class="stringliteral">&quot;canOpenChannel&quot;</span>, (<a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a>)hnd);</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="comment">// Using our new shiny handle, we specify the baud rate</span></div><div class="line">  <span class="comment">// using one of the convenient canBITRATE_xxx constants.</span></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="comment">// The bit layout is in depth discussed in most CAN</span></div><div class="line">  <span class="comment">// controller data sheets, and on the web at</span></div><div class="line">  <span class="comment">// http://www.kvaser.se.</span></div><div class="line">  <span class="comment">//</span></div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (Bitrate != 0) {</div><div class="line">    stat = <a class="code" href="group___c_a_n.html#ga7eb8c2e92cfae57e7ec5031818524301">canSetBusParams</a>(hnd, Bitrate, 0, 0, 0, 0, 0);</div><div class="line">    <span class="keywordflow">if</span> (stat &lt; 0) {</div><div class="line">      ErrorExit(<span class="stringliteral">&quot;canSetBusParams&quot;</span>, stat);</div><div class="line">    }</div><div class="line">  } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (BitrateFdA != 0 &amp;&amp; BitrateFdD != 0) {</div><div class="line">    stat = <a class="code" href="group___c_a_n.html#ga7eb8c2e92cfae57e7ec5031818524301">canSetBusParams</a>(hnd, BitrateFdA, 0, 0, 0, 0, 0);</div><div class="line">    <span class="keywordflow">if</span> (stat &lt; 0) {</div><div class="line">      ErrorExit(<span class="stringliteral">&quot;canSetBusParams&quot;</span>, stat);</div><div class="line">    }</div><div class="line">    stat = <a class="code" href="group___c_a_n.html#gafcd85fbac103dcb123f4cd609be6fa14">canSetBusParamsFd</a>(hnd, BitrateFdD, 0, 0, 0);</div><div class="line">    <span class="keywordflow">if</span> (stat &lt; 0) {</div><div class="line">      ErrorExit(<span class="stringliteral">&quot;canSetBusParamsFd&quot;</span>, stat);</div><div class="line">    }</div><div class="line">  } <span class="keywordflow">else</span> {</div><div class="line">    Usage();</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="comment">// Then we start the ball rolling.</span></div><div class="line">  <span class="comment">//</span></div><div class="line">  stat = <a class="code" href="group___c_a_n.html#ga99c7c99cc71580f8099a1407f4f9ea1a">canBusOn</a>(hnd);</div><div class="line">  <span class="keywordflow">if</span> (stat &lt; 0) {</div><div class="line">    ErrorExit(<span class="stringliteral">&quot;canBusOn&quot;</span>, stat);</div><div class="line">    exit(1);</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// Return the handle; our caller will need it for</span></div><div class="line">  <span class="comment">// further exercising the CAN controller.</span></div><div class="line">  <span class="keywordflow">return</span> hnd;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment"></span><span class="comment">//</span></div><div class="line"><span class="keywordtype">void</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* argv[])</div><div class="line">{</div><div class="line">  <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat;</div><div class="line">  <span class="keywordtype">int</span> i;</div><div class="line">  <a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a> hnd;</div><div class="line">  FILE *f;</div><div class="line">  <span class="keywordtype">int</span> Ready;</div><div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> MessageCount;</div><div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> timeOffset;</div><div class="line">  <span class="keywordtype">int</span> Overrun;</div><div class="line"></div><div class="line">  Filename[0] = <span class="charliteral">&#39;\0&#39;</span>;</div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="comment">// First, parse the command-line arguments.</span></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="keywordflow">for</span> (i=1; i&lt;argc; i++) {</div><div class="line">    <span class="keywordtype">int</span> tmp;</div><div class="line">    <span class="keywordtype">char</span> c;</div><div class="line">    <span class="keywordflow">if</span> (sscanf(argv[i], <span class="stringliteral">&quot;-%d%c&quot;</span>, &amp;tmp, &amp;c) == 1) {</div><div class="line">      Source = tmp;</div><div class="line">    } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (sscanf(argv[i], <span class="stringliteral">&quot;-B%d%c&quot;</span>, &amp;tmp, &amp;c) == 1) {</div><div class="line">      <span class="comment">// We&#39;ll use the canBITRATE_xxx constants here for simplicity;</span></div><div class="line">      <span class="comment">// of course CANLIB supports other bit rates but writing that code</span></div><div class="line">      <span class="comment">// is left as an exercise.</span></div><div class="line">      <span class="keywordflow">switch</span> (tmp) {</div><div class="line">      <span class="keywordflow">case</span> 1000 : Bitrate = <a class="code" href="canlib_8h.html#ac823cae7d94763607a7ca40fcdb5196d">canBITRATE_1M</a>; <span class="keywordflow">break</span>;</div><div class="line">      <span class="keywordflow">case</span> 500  : Bitrate = <a class="code" href="canlib_8h.html#ae72ee3c12379c4817de3b53c7cc6d89c">canBITRATE_500K</a>; <span class="keywordflow">break</span>;</div><div class="line">      <span class="keywordflow">case</span> 250  : Bitrate = <a class="code" href="canlib_8h.html#a87f618db13ec4f48610def72cfb40969">canBITRATE_250K</a>; <span class="keywordflow">break</span>;</div><div class="line">      <span class="keywordflow">case</span> 125  : Bitrate = <a class="code" href="canlib_8h.html#a29a9aafde6e861b4fffb72ad124aeed1">canBITRATE_125K</a>; <span class="keywordflow">break</span>;</div><div class="line">      default : Usage();</div><div class="line">      }</div><div class="line">    } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (sscanf(argv[i], <span class="stringliteral">&quot;-A%d%c&quot;</span>, &amp;tmp, &amp;c) == 1) {</div><div class="line">      <span class="keywordflow">switch</span> (tmp) {</div><div class="line">      <span class="keywordflow">case</span> 1000 : BitrateFdA = <a class="code" href="canlib_8h.html#a3a93c64ba76ebe3c5e525d4dfa4ade8d">canFD_BITRATE_1M_80P</a>; <span class="keywordflow">break</span>;</div><div class="line">      <span class="keywordflow">case</span>  500 : BitrateFdA = <a class="code" href="canlib_8h.html#a71dec626b7de7ddd18daf93e535902f4">canFD_BITRATE_500K_80P</a>; <span class="keywordflow">break</span>;</div><div class="line">      default : Usage();</div><div class="line">      }</div><div class="line">    } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (sscanf(argv[i], <span class="stringliteral">&quot;-D%d%c&quot;</span>, &amp;tmp, &amp;c) == 1) {</div><div class="line">      <span class="keywordflow">switch</span> (tmp) {</div><div class="line">      <span class="keywordflow">case</span> 8000 : BitrateFdD = <a class="code" href="canlib_8h.html#ac1d8bd40554b1433f00e4f1bca3f7a8e">canFD_BITRATE_8M_60P</a>; <span class="keywordflow">break</span>;</div><div class="line">      <span class="keywordflow">case</span> 4000 : BitrateFdD = <a class="code" href="canlib_8h.html#aa643e5939f0d4d5fff21747e55c76153">canFD_BITRATE_4M_80P</a>; <span class="keywordflow">break</span>;</div><div class="line">      <span class="keywordflow">case</span> 2000 : BitrateFdD = <a class="code" href="canlib_8h.html#a9f21214c84dbb6f5f37eb27520ecc162">canFD_BITRATE_2M_80P</a>; <span class="keywordflow">break</span>;</div><div class="line">      <span class="keywordflow">case</span> 1000 : BitrateFdD = <a class="code" href="canlib_8h.html#a3a93c64ba76ebe3c5e525d4dfa4ade8d">canFD_BITRATE_1M_80P</a>; <span class="keywordflow">break</span>;</div><div class="line">      default : Usage();</div><div class="line">      }</div><div class="line">    } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp(argv[i], <span class="stringliteral">&quot;-q&quot;</span>) == 0) {</div><div class="line">      Quiet++;</div><div class="line">    } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp(argv[i], <span class="stringliteral">&quot;-fd&quot;</span>) == 0) {</div><div class="line">      can_open_flags |= <a class="code" href="canlib_8h.html#a10aad075bfb9891aa98cf3c131977dc3">canOPEN_CAN_FD</a>;</div><div class="line">    } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp(argv[i], <span class="stringliteral">&quot;-virtual&quot;</span>) == 0) {</div><div class="line">      can_open_flags |= <a class="code" href="canlib_8h.html#a8e4ead14e67e49e27674c706e0d88f23">canOPEN_ACCEPT_VIRTUAL</a>;</div><div class="line">    } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp(argv[i], <span class="stringliteral">&quot;-h&quot;</span>) == 0) {</div><div class="line">      Usage();</div><div class="line">    } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (argv[i][0] != <span class="charliteral">&#39;-&#39;</span>) {</div><div class="line">      strcpy(Filename, argv[i]);</div><div class="line">    } <span class="keywordflow">else</span> {</div><div class="line">      Usage();</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (Quiet == 0) {</div><div class="line">    fprintf(stderr, <span class="stringliteral">&quot;Logging to %s (candump -h for help; ESC to quit.)\n&quot;</span>,</div><div class="line">            Filename[0]? Filename : <span class="stringliteral">&quot;standard output&quot;</span>);</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="comment">// Initialize CANLIB.</span></div><div class="line">  <span class="comment">//</span></div><div class="line">  <a class="code" href="group__can__general.html#gaff1ec1d3416d3bdd56336a7b9ac008b1">canInitializeLibrary</a>();</div><div class="line"></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="comment">// Setup the selected CAN controller.</span></div><div class="line">  <span class="comment">//</span></div><div class="line">  hnd = InitCtrl(Source);</div><div class="line"></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="comment">// Open (i.e create) the log file.</span></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="keywordflow">if</span> (strlen(Filename) &gt; 0) {</div><div class="line">    f = fopen(Filename, <span class="stringliteral">&quot;w&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span> (!f) {</div><div class="line">      fprintf(stderr, <span class="stringliteral">&quot;Can&#39;t create log file &#39;%s&#39;.\n&quot;</span>, Filename);</div><div class="line">      <a class="code" href="group___c_a_n.html#ga49525373a4d08d93c651ec10f79dd36b">canClose</a>(hnd);</div><div class="line">      exit(1);</div><div class="line">    }</div><div class="line">  } <span class="keywordflow">else</span> {</div><div class="line">    f = stdout;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="comment">// Read the timer so we can print normalized time stamps later on.</span></div><div class="line">  <span class="comment">// Note that we are on-bus now, so IF some messages have arrived already,</span></div><div class="line">  <span class="comment">// they will be logged with a negative time value.</span></div><div class="line">  <span class="comment">//</span></div><div class="line">  timeOffset = <a class="code" href="canlib_8h.html#a04c2f80a23b992bf17591749192e8d48">canReadTimer</a>(hnd);</div><div class="line"></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="comment">// Print a little header, unless we are requested not to do so.</span></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="keywordflow">if</span> (Quiet == 0) {</div><div class="line">    time_t t;</div><div class="line">    time(&amp;t);</div><div class="line">    fprintf(f, <span class="stringliteral">&quot;; Logging started at %s&quot;</span>, ctime(&amp;t));</div><div class="line">    fprintf(f, <span class="stringliteral">&quot;; (x = Extended Id, R = Remote Frame, o = Overrun, N = NERR)\n&quot;</span>);</div><div class="line">    fprintf(f, <span class="stringliteral">&quot;; Ident xRoN DLC  Data 0........................7    Time\n&quot;</span>);</div><div class="line">  }</div><div class="line"></div><div class="line">  Ready = FALSE;</div><div class="line">  MessageCount = 0;</div><div class="line">  Overrun = FALSE;</div><div class="line"></div><div class="line">  <span class="keywordflow">while</span> (!Ready) {</div><div class="line"></div><div class="line">    <span class="keywordflow">while</span> (!_kbhit()) {</div><div class="line">      <span class="keywordtype">long</span> id;</div><div class="line">      <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> dlc, flags;</div><div class="line">      <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> msg[64];</div><div class="line">      <a class="code" href="linlib_8h.html#a798af1e30bc65f319c1a246cecf59e39">DWORD</a> time;</div><div class="line">      <span class="keywordtype">long</span> t, timeFrac, timeInt;</div><div class="line">      <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i;</div><div class="line"></div><div class="line">      <span class="keywordflow">do</span> {</div><div class="line">        <span class="comment">//</span></div><div class="line">        <span class="comment">// Read a message.</span></div><div class="line">        <span class="comment">//</span></div><div class="line">        stat = <a class="code" href="group___c_a_n.html#ga157d49a9343dea68ac953145e02266d8">canRead</a>(hnd, &amp;<span class="keywordtype">id</span>, msg, &amp;dlc, &amp;flags, &amp;time);</div><div class="line">        <span class="keywordflow">if</span> (stat == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a> &amp;&amp; (Quiet &lt;= 1)) {</div><div class="line">          <span class="keywordflow">if</span> ((flags &amp; <a class="code" href="canstat_8h.html#ade1b8ed815a4bc6ada6ec666e48e9cf8">canMSG_ERROR_FRAME</a>) == 0) {</div><div class="line">            <span class="comment">// A message.</span></div><div class="line">            <span class="comment">// Print identifier and flags.</span></div><div class="line">            fprintf(f, <span class="stringliteral">&quot;%8ld%c%c%c%c%c%c  %02u &quot;</span>,</div><div class="line">                    <span class="keywordtype">id</span>,</div><div class="line">                    (flags &amp; <a class="code" href="canstat_8h.html#ade936f23b4b303d64187b4c6589c9c10">canMSG_EXT</a>)        ? <span class="charliteral">&#39;x&#39;</span> : <span class="charliteral">&#39; &#39;</span>,</div><div class="line">                    (flags &amp; <a class="code" href="canstat_8h.html#a687df04559a9995e0bec503655454eb3">canMSG_RTR</a>)        ? <span class="charliteral">&#39;R&#39;</span> : <span class="charliteral">&#39; &#39;</span>,</div><div class="line">                    (flags &amp; <a class="code" href="canstat_8h.html#adf0d578c7b25ede29a9733da44f08ab3">canMSGERR_OVERRUN</a>) ? <span class="charliteral">&#39;o&#39;</span> : <span class="charliteral">&#39; &#39;</span>,</div><div class="line">                    (flags &amp; <a class="code" href="canstat_8h.html#a1f53f2fdeeb263cd2bf5d374e1dd59a2">canMSG_NERR</a>)       ? <span class="charliteral">&#39;N&#39;</span> : <span class="charliteral">&#39; &#39;</span>, <span class="comment">// TJA 1053/1054 transceivers only</span></div><div class="line">                    (flags &amp; <a class="code" href="canstat_8h.html#a162be4f6b66c81fa09dc76c268475216">canFDMSG_EDL</a>)      ? <span class="charliteral">&#39;F&#39;</span> : <span class="charliteral">&#39; &#39;</span>,</div><div class="line">                    (flags &amp; <a class="code" href="canstat_8h.html#a20c4f6ae319558189eb85d10036e9eea">canFDMSG_BRS</a>)      ? <span class="charliteral">&#39;B&#39;</span> : <span class="charliteral">&#39; &#39;</span>,</div><div class="line">                    dlc);</div><div class="line">          } <span class="keywordflow">else</span> {</div><div class="line">            <span class="comment">// An error frame.</span></div><div class="line">            <span class="comment">// CANLIB will set the id to something controller</span></div><div class="line">            <span class="comment">// dependent.</span></div><div class="line">            fprintf(f, <span class="stringliteral">&quot;  (%04lx) Error    &quot;</span>, <span class="keywordtype">id</span>);</div><div class="line">          }</div><div class="line"></div><div class="line">          <span class="comment">// Print the data bytes, but not for Remote Frames.</span></div><div class="line">          <span class="comment">// Print at most 8 bytes - the DLC might be larger</span></div><div class="line">          <span class="comment">// but there are never more than 8 bytes in a</span></div><div class="line">          <span class="comment">// message.</span></div><div class="line">          <span class="keywordflow">if</span> (flags &amp; <a class="code" href="canstat_8h.html#a162be4f6b66c81fa09dc76c268475216">canFDMSG_EDL</a>) {</div><div class="line">            <span class="keywordflow">if</span> ((flags &amp; canMSG_RTR) == 0) {</div><div class="line">              <span class="keywordflow">for</span> (i=0; i &lt; dlc; i++) fprintf(f, <span class="stringliteral">&quot;%3u &quot;</span>, msg[i]);</div><div class="line">              <span class="keywordflow">for</span> (; i&lt;8; i++) fprintf(f, <span class="stringliteral">&quot;    &quot;</span>);</div><div class="line">            } <span class="keywordflow">else</span> {</div><div class="line">              fprintf(f, <span class="stringliteral">&quot;                                &quot;</span>);</div><div class="line">            }</div><div class="line">          }</div><div class="line">          <span class="keywordflow">else</span> {</div><div class="line">            <span class="keywordflow">if</span> ((flags &amp; canMSG_RTR) == 0) {</div><div class="line">              <span class="keywordflow">for</span> (i=0; i &lt; ((dlc &gt; 8)? 8: dlc); i++) fprintf(f, <span class="stringliteral">&quot;%3u &quot;</span>, msg[i]);</div><div class="line">              <span class="keywordflow">for</span> (; i&lt;8; i++) fprintf(f, <span class="stringliteral">&quot;    &quot;</span>);</div><div class="line">            } <span class="keywordflow">else</span> {</div><div class="line">              fprintf(f, <span class="stringliteral">&quot;                                &quot;</span>);</div><div class="line">            }</div><div class="line">          }</div><div class="line"></div><div class="line">          <span class="comment">// Print the time stamp, formatted as seconds.</span></div><div class="line">          t = time - timeOffset;</div><div class="line">          <span class="keywordflow">if</span> (t &lt; 0) {</div><div class="line">            <span class="comment">// Yes, negative values may occur.. see above.</span></div><div class="line">            timeFrac = (-t) % 1000;</div><div class="line">            timeInt  = (-t) / 1000;</div><div class="line">            fprintf(f, <span class="stringliteral">&quot;-%5ld.%03ld\n&quot;</span>, timeInt, timeFrac);</div><div class="line">          } <span class="keywordflow">else</span> {</div><div class="line">            timeFrac = t % 1000;</div><div class="line">            timeInt  = t / 1000;</div><div class="line">            fprintf(f, <span class="stringliteral">&quot;%6ld.%03ld\n&quot;</span>, timeInt, timeFrac);</div><div class="line">          }</div><div class="line"></div><div class="line">          <span class="comment">// Keep some statistics.</span></div><div class="line">          <span class="keywordflow">if</span> (flags &amp; canMSGERR_OVERRUN) Overrun = TRUE;</div><div class="line">          MessageCount++;</div><div class="line">        }</div><div class="line">        <span class="comment">//</span></div><div class="line">        <span class="comment">// When there are no more messages in the queue,</span></div><div class="line">        <span class="comment">// stat will be equal to canERR_NOMSG.</span></div><div class="line">        <span class="comment">//</span></div><div class="line">      } <span class="keywordflow">while</span> (stat == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>);</div><div class="line">      <span class="comment">// Have a nap while the driver accumulates messages for us..</span></div><div class="line">      Sleep(50);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">//</span></div><div class="line">    <span class="comment">// React to whatever the user has entered at the keyboard.</span></div><div class="line">    <span class="comment">//</span></div><div class="line">    <span class="keywordflow">switch</span> (_getch()) {</div><div class="line">    <span class="keywordflow">case</span> 3:</div><div class="line">    <span class="keywordflow">case</span> 27:</div><div class="line">      Ready = TRUE;</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line">    <span class="keywordflow">case</span> <span class="charliteral">&#39;q&#39;</span>:</div><div class="line">      <span class="keywordflow">if</span> (Quiet &gt; 0) Quiet--;</div><div class="line">      fprintf(stderr, <span class="stringliteral">&quot;Quiet=%d\n&quot;</span>, Quiet);</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line">    <span class="keywordflow">case</span> <span class="charliteral">&#39;Q&#39;</span>:</div><div class="line">      <span class="keywordflow">if</span> (Quiet &lt; 5) Quiet++;</div><div class="line">      fprintf(stderr, <span class="stringliteral">&quot;Quiet=%d\n&quot;</span>, Quiet);</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="comment">// Print a little footer, unless we are requested not to do so.</span></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="keywordflow">if</span> (Quiet == 0) {</div><div class="line">    time_t t;</div><div class="line">    time(&amp;t);</div><div class="line">    fprintf(f, <span class="stringliteral">&quot;; Logging ended at %s&quot;</span>, ctime(&amp;t));</div><div class="line">    fprintf(f, <span class="stringliteral">&quot;; %lu messages.%s\n&quot;</span>,</div><div class="line">            MessageCount,</div><div class="line">            Overrun? <span class="stringliteral">&quot; There were overruns.&quot;</span> : <span class="stringliteral">&quot;&quot;</span>);</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="comment">// Clean up.</span></div><div class="line">  <span class="comment">//</span></div><div class="line">  fclose(f);</div><div class="line">  (void)<a class="code" href="group___c_a_n.html#gaf1786cfbfd542b18b9c599d278837bd9">canBusOff</a>(hnd);</div><div class="line">  (void)<a class="code" href="group___c_a_n.html#ga49525373a4d08d93c651ec10f79dd36b">canClose</a>(hnd);</div><div class="line"></div><div class="line">}</div><div class="line"></div></div><!-- fragment --> </div></div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="page_canlib.html">CAN bus API (CANlib)</a></li><li class="navelem"><a class="el" href="page_user_guide_canlib_samples.html">Sample Programs (CANlib)</a></li>
    <li class="footer"> (canlib 5.38) Tue Feb 15 2022</li>
  </ul>
</div>
</body>
</html>
